package com.archerdu.apply.controller.admin;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.ProductCatalog;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.ProductCatalogManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.commons.constant.DataConstant;

@Controller
@Scope("prototype")
@RequestMapping(value = "/admin/productCatalog")
public class ProductCatalogController
        extends BaseController {
    
    private static final long  serialVersionUID = 4707162553670697575L;
    
    private ProductCatalogManager productCatalogManager;
    private ProductCatalog        productCatalog;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/tree")
    public String tree() {
        return "admin/productCatalog/tree";
    }
    
    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, ProductCatalogController mc) {
        productCatalog = mc.getProductCatalog();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin/productCatalog/add");
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            mv.addObject(productCatalog);
        }
        else {
            try {
                productCatalog.setCreateUser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER))
                        .getId());
                productCatalog.setCreatetime(getSqlDate());
                productCatalogManager.add(productCatalog);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            }
            catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        return mv;
    }
    
    /**
     * 文章目录编辑
     * 
     * @param requ
     * @param productCatalog
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, ProductCatalogController mc) {
        
        logger.debug("productCatalog.id is " + mc.getProductCatalog().getId());
        productCatalog = mc.getProductCatalog();
        
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(productCatalog.getId())) {
                productCatalog = productCatalogManager.get(productCatalog.getId());
            }
        }
        else {
            productCatalog.setUpdateUser(((User) requ.getSession().getAttribute((DataConstant.SESSION_SYS_USER))).getId());
            productCatalog.setUpdatetime(getSqlDate());
            try {
                productCatalogManager.modify(productCatalog);
                productCatalog = productCatalogManager.get(productCatalog.getId());
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            }
            catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        
        ModelAndView mav = new ModelAndView();
        mav.addObject(getMessage());
        mav.addObject(productCatalog).setViewName("admin/productCatalog/edit");
        return mav;
    }
    
    /**
     * 目录json字符串
     * 
     * @param response
     * @param writer
     * @param acc
     */
    @RequestMapping(value = "/json")
    public void json(HttpServletResponse response, Writer writer, ProductCatalogController acc) {
        response.setContentType("text/plain");
        response.setCharacterEncoding("utf-8");
        productCatalog = acc.getProductCatalog();
        if (productCatalog != null && productCatalog.getId() != null) {
            productCatalog = productCatalogManager.get(productCatalog.getId());
        }
        else {
            productCatalog = new ProductCatalog();
            productCatalog.setId("0");
        }
        try {
            //writer.write(productCatalogManager.loadJson(productCatalog));
            writer.write(productCatalogManager.loadJson(productCatalog));
        }
        catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
    
    public ProductCatalogManager getProductCatalogManager() {
        return productCatalogManager;
    }
    
    public void setProductCatalogManager(ProductCatalogManager productCatalogManager) {
        this.productCatalogManager = productCatalogManager;
    }
    
    public ProductCatalog getProductCatalog() {
        return productCatalog;
    }
    
    public void setProductCatalog(ProductCatalog productCatalog) {
        this.productCatalog = productCatalog;
    }
    
}
